/*
 * CN_PARTY_READY.cpp
 *
 *  Created on: 2013. 5. 21.
 *      Author: the2
 */


#include "../../User.h"
#include "dungeon.h"

#include "../../NLogic/Static.h"
#include "../../NLogic/Battle.h"
#include "../../NGameLog/Static.h"

namespace ProjectZ { namespace NCommand { namespace NDungeon {

void CN_PARTY_READY::OnExecute(User * pUser, ::Network::Packet * pPacket)
{
	/*
	 * CN_PARTY_READY		0x110a
	{
		u8	ready;		// 0:false, 1:true
		u32	hp_max;
		u32	u32sp_max;
	}

	SN_PARTY_READY		0x110b
	{
		u32	gid;
		u8	ready;		// 0:false, 1:true
	}

	SN_PARTY_START		0x110c
	{
	}

	 */
	STRESSLOG("CN_PARTY_READY");

	PREPARE_PACKET;

	NLogic::Space * pSpace = pUser->GetSpace();
	if (NULL == pSpace)
	{
		DEBUGLOG("CN_PARTY_READY NULL == pSpace");
		return;
	}

	if (false == pSpace->IsDungeon() )
	{
		DEBUGLOG("CN_PARTY_READY space is not single or party dungeion");
		return;
	}

	NLogic::Battle * pBattle = pUser->GetBattle();
	if (NULL == pBattle)
	{
		DEBUGLOG("NULL == pBattle");
		return;
	}

	uint8_t ready = 0;
	int hp = 0, sp = 0;

	* pPacket >> ready >> hp >> sp;

//	pUser->player_info.HP = hp;
//	pUser->player_info.SP = sp;

	pUser->GetBattle()->GetData().HP = hp;
	pUser->GetBattle()->GetData().SP = sp;

	// 최초 만 max hp 셋팅
	if (pBattle->GetData()._firstReady == true)
	{
//		pUser->player_info.HP_MAX = hp;
//		pUser->player_info.SP_MAX = sp;

		pUser->GetBattle()->GetData().HP_MAX = hp;
		pUser->GetBattle()->GetData().SP_MAX = sp;

		pBattle->GetData()._firstReady = false;
	}

	if (false == pSpace->IsWorldBoss())
	{
		int tid = (pSpace->GetData()._table_id - 1) * 3 + pSpace->GetData()._difficulty;

		User::DUNGEON_CREAR_INFO & dungeonClearInfo = pUser->GetDungeonClearInfo();
		int retValue = 0;
		if (true == dungeonClearInfo.GetValue(tid, retValue) && 0 == retValue)
		{
			if (false == dungeonClearInfo.SetValue(tid, 2))
			{
				DEBUGLOG("ERROR %s TID : %d", __FUNCTION__, tid);
			}
		}
	}

	if (true == pSpace->IsSingleDungeon())
	{
		::Network::Packet * pNoti = ::Network::GETPACKETPOOL()->SEND_NEW();
		* pNoti << pUser->GetUserSeq();
		* pNoti << ready;
		pNoti->MakePacket(CMD_SN_PARTY_READY);
		pUser->Send(pNoti);

		::Network::Packet * pNoti2 = ::Network::GETPACKETPOOL()->SEND_NEW();
		pNoti2->MakePacket(CMD_SN_PARTY_START);
		pUser->Send(pNoti2);

		if (true == pSpace->GetData()._isFirstStart)
		{
//			pSpace->GetData()._starttick = NF::GetTickCount();
			pSpace->GetData()._starttick = time(NULL);
			pSpace->GetData()._isFirstStart = false;

			::ProjectZ::NGameLog::Bson * pBson = ::ProjectZ::NGameLog::Static::Instance().GETBSONPOOL()->NEW();
			if(NULL != pBson)
			{
				pBson->SetCollections("log_dungeon_play");
				bson * bson = pBson->GetBsonData();
				bson_append_int( bson, "user_seq", pUser->GetUserSeq() );
				bson_append_int( bson, "company", pUser->GetCompany() );
				bson_append_int( bson, "sale_code", pUser->GetSaleCode() );
				bson_append_int( bson, "character_class", pUser->GetCharacterInfo().classtype );
				bson_append_int( bson, "character_level", pUser->GetCharacterInfo().level );
				bson_append_int( bson, "space_num", pSpace->GetData()._index );
				bson_append_int( bson, "start_time", pSpace->GetData()._starttick );
				bson_append_int( bson, "end_time", pSpace->GetData()._finishtick );
				bson_append_int( bson, "play_time", 0 );
				bson_append_int( bson, "dungeon_type", static_cast<int>(pSpace->GetData()._spaceType) );
				bson_append_int( bson, "dungeon_tid", pSpace->GetData()._table_id );
				bson_append_int( bson, "dungeon_mod", pSpace->GetData()._difficulty );
				bson_append_int( bson, "exp", pUser->GetBattle()->GetData().hunting_exp );
				bson_append_int( bson, "gold", pUser->GetBattle()->GetData().get_gold );
				bson_append_int( bson, "item_seq", 0 );
				bson_append_time_t( bson, "reg_time", time(NULL) );

				::ProjectZ::NGameLog::MongoDB * pMongoDB = ::ProjectZ::NGameLog::Static::Instance().GetMongoDB();
				pMongoDB->Push(pBson);
			}
		}

		return;
	}
	else if (true == pSpace->IsPartyDungeon())
	{

		NLogic::Party * pParty = pUser->GetParty();
		if (NULL == pParty)
		{
			DEBUGLOG("CN_PARTY_READY NULL == pParty");
			return;
		}

		if (pParty->GetState() == NLogic::Party::State::READY_WAIT)
		{
			::Network::Packet * pNoti = ::Network::GETPACKETPOOL()->SEND_NEW();
			* pNoti << pUser->GetUserSeq();
			* pNoti << ready;
			pNoti->MakePacket(CMD_SN_PARTY_READY);
			pSpace->SendAll(pNoti);

			pParty->AddReadyUserTree(pUser);
			if (pParty->GetAcceptUserTree().size() >= pParty->GetReadyUserTree().size())
			{
				auto itr = pParty->GetAcceptUserTree().find(pUser->GetUserSeq());
				if (itr != pParty->GetAcceptUserTree().end())
					pParty->GetAcceptUserTree().erase(itr);

				if (pParty->GetAcceptUserTree().size() > 0)
					return;
			}

			pParty->GetAcceptUserTree().clear();
			DEBUGLOG("CN_PARTY_READY SEND SN_PARTY_START");
			::Network::Packet * pNoti2 = ::Network::GETPACKETPOOL()->SEND_NEW();
			pNoti2->MakePacket(CMD_SN_PARTY_START);
			pSpace->SendAll(pNoti2);

			pParty->ReadyUserTreeMoveToPlayUserTree();
			pParty->SetState(NLogic::Party::State::DUNGEON_BATTLE);

			if (true == pSpace->GetData()._isFirstStart)
			{
				//				pSpace->GetData()._starttick = NF::GetTickCount();
				pSpace->GetData()._starttick = time(NULL);
				pSpace->GetData()._isFirstStart = false;

				std::for_each(pParty->GetAllUserTree().begin(), pParty->GetAllUserTree().end(), [&](NLogic::Party::USERTREE::value_type & pair){
					User * pUser = pair.second;
					::ProjectZ::NGameLog::Bson * pBson = ::ProjectZ::NGameLog::Static::Instance().GETBSONPOOL()->NEW();
					if(NULL != pBson)
					{
						pBson->SetCollections("log_dungeon_play");
						bson * bson = pBson->GetBsonData();
						bson_append_int( bson, "user_seq", pUser->GetUserSeq() );
						bson_append_int( bson, "company", pUser->GetCompany() );
						bson_append_int( bson, "sale_code", pUser->GetSaleCode() );
						bson_append_int( bson, "character_class", pUser->GetCharacterInfo().classtype );
						bson_append_int( bson, "character_level", pUser->GetCharacterInfo().level );
						bson_append_int( bson, "space_num", pSpace->GetData()._index );
						bson_append_int( bson, "start_time", pSpace->GetData()._starttick );
						bson_append_int( bson, "end_time", pSpace->GetData()._finishtick );
						bson_append_int( bson, "play_time", 0 );
						bson_append_int( bson, "dungeon_type", static_cast<int>(pSpace->GetData()._spaceType) );
						bson_append_int( bson, "dungeon_tid", pSpace->GetData()._table_id );
						bson_append_int( bson, "dungeon_mod", pSpace->GetData()._difficulty );
						bson_append_int( bson, "exp", pUser->GetBattle()->GetData().hunting_exp );
						bson_append_int( bson, "gold", pUser->GetBattle()->GetData().get_gold );
						bson_append_int( bson, "item_seq", 0 );
						bson_append_time_t( bson, "reg_time", time(NULL) );

						::ProjectZ::NGameLog::MongoDB * pMongoDB = ::ProjectZ::NGameLog::Static::Instance().GetMongoDB();
						pMongoDB->Push(pBson);
					}
				});
			}
		}
	}
	else
	{
		DEBUGLOG("CN_PARTY_READY SPACE IS NOT DUNGEON");
	}
}

} /*NDungeon*/ } /*NCommand*/ } /*ProjectZ*/
